"""
StarAlchemy Models that could be useful.
"""
import datetime

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, Date, Time

Base = declarative_base()

class DateDimension(Base):
   __tablename__ = 'date_dimension'

   id = Column(Integer, primary_key=True)
   date = Column(Date)
   #calendar_week_ending_date = Column(Date)
   month = Column(Integer)
   month_name = Column(String)
   day = Column(Integer)
   day_of_week = Column(Integer)
   day_of_week_name = Column(String)
   week_number = Column(Integer)
   year = Column(Integer)
   day_of_year = Column(Integer)

   @classmethod
   def from_datetime(kls, date):
       instance = kls()

       if isinstance(date, datetime.date) and isinstance(date, datetime.datetime): #accept both a datetime.datetime and datetime.date object
           date = date.date()
       instance.date = date
       #instance.calendar_week_ending_date = date #TODO fix this
       instance.month = date.month
       instance.month_name = date.strftime("%B")
       instance.day = date.day
       instance.day_of_week = date.weekday()
       instance.day_of_week_name = {0:'Monday',
                                    1:'Tuesday',
                                    2:'Wednesday',
                                    3:'Thursday',
                                    4:'Friday',
                                    5:'Saturday',
                                    6:'Sunday'}[date.weekday()]
       instance.week_number = int(date.strftime("%W"))
       instance.year = date.year
       instance.day_of_year = int(date.strftime("%j"))
       
       return instance


   def __repr__(self):
       return "<DateDimension('%s',)>" % (self.date)


class TimeDimension(Base):
   __tablename__ = 'time_dimension'

   id = Column(Integer, primary_key=True)
   time = Column(Time)
   hour = Column(Integer)
   minute = Column(Integer)
   second = Column(Integer)
   

   @classmethod
   def from_datetime(kls, time):
       instance = kls()
       if not isinstance(time, datetime.time): #accept both a datetime.datetime and datetime.time object
           time = time.time()
       instance.time = time
       instance.hour = time.hour
       instance.minute = time.minute
       instance.second = time.second

       return instance


   def __repr__(self):
       return "<TimeDimension('%s',)>" % (self.time)
